/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;


public abstract class CompoundWordTokenFilter extends ElasticFilter{

	
	public static final String WORDS_SEPARATOR = ",";
	public static final String WORD_LIST_PARAM_KEY = "word_list";
	public static final String WORD_LIST_PATH_PARAM_KEY = "word_list_path";
	
	
	
	private Set<String> wordList;
	private String wordsListPath;
	
	
	
	
	public CompoundWordTokenFilter(ElasticFilterType elasticFilterType,String defName) {
		this(null, elasticFilterType, defName);
	}

	public CompoundWordTokenFilter(Map<String,String> params, ElasticFilterType elasticFilterType,String defName) {
		super(elasticFilterType, defName);
		
		if(params!=null){
			String stopwStr = params.get(WORD_LIST_PARAM_KEY);
			if(stopwStr!=null && !stopwStr.isEmpty()){
				this.wordList=new HashSet<String>();
				String[] stopwds = stopwStr.split(WORDS_SEPARATOR);
				for (String stopw : stopwds) {
					this.wordList.add(stopw.trim());
				}
			}
			this.wordsListPath = params.get(WORD_LIST_PATH_PARAM_KEY); 
		}
	}

	
	

	@Override
	public boolean hasCustomParams() {
		 if(this.wordList!= null || 
			(this.wordsListPath!=null && !this.wordsListPath.isEmpty())
		 )return true;
		 return false;
	}
	
	
	
	
	@Override
	public XContentBuilder addToBuilder(XContentBuilder mappingBuilder) throws IOException {
		mappingBuilder.startObject(getDefinitionName())
		.field("type", getComponentSubTypeName());
		
		//add custom params
		if(this.wordList!=null){
			List<String> stopWrdsList = new ArrayList<String>(this.wordList);
			mappingBuilder.field(WORD_LIST_PARAM_KEY, stopWrdsList);
		}
		if(this.wordsListPath != null && !this.wordsListPath.isEmpty()){
			mappingBuilder.field(WORD_LIST_PATH_PARAM_KEY, this.wordsListPath);
		}
		return mappingBuilder;
	}
	

	
	
	@Override
	public void setParamsFromSettings(Settings settings) {
		Settings stopWordsSettings = settings.getByPrefix(WORD_LIST_PARAM_KEY+".");
		if (stopWordsSettings.getAsMap().size()>0)this.wordList = new HashSet<String>();
		for (Entry<String, String> stopWordSetting : stopWordsSettings.getAsMap().entrySet()) {
			this.wordList.add(stopWordSetting.getValue());
		}
		
		String stopWordsPathSetting = settings.get(WORD_LIST_PATH_PARAM_KEY);
		if(stopWordsPathSetting!=null && !stopWordsPathSetting.isEmpty()){
			this.wordsListPath = stopWordsPathSetting;
		}
	}
	
	

	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition other){
		
		if(other==null)return false;
		CompoundWordTokenFilter o;
		try {
			o = (CompoundWordTokenFilter)other;
		} catch (ClassCastException e) {
			return false;
		}
		
		return 
		EqualsUtil.areEqual(this.wordList, o.wordList)
		&&
		EqualsUtil.areEqual(this.wordsListPath, o.wordsListPath);
	}
	
}
